package com.yang.doctorrecruitment.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.yang.doctorrecruitment.dto.UserLogin;
import com.yang.doctorrecruitment.utils.DBManager;


public class UserloginDAO {

      public static boolean findUserNameAndPassWord(String UserName,String PassWord){
		Connection conn=DBManager.getconnection();
    	   ResultSet res=null;
    	   PreparedStatement state=null;
    	   String sql="select * from userlogin where UserName=? and PassWord=?";
    	   try {
			state=conn.prepareStatement(sql);
			state.setString(1, UserName);
			state.setString(2, PassWord);
			res=state.executeQuery();
			if(res.next()){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}finally{
			DBManager.closeAll(conn,state,res);
		}
    	  return false;
      }
      public static boolean findUserByUserName(String UserName){
    	  ResultSet  res=null;
    	  PreparedStatement state=null;
		  Connection conn=DBManager.getconnection();
    	  String sql="select * from userlogin where UserName=?";
    	  try{
    		  state=conn.prepareStatement(sql);
    		  state.setString(1,UserName);
    		  res=state.executeQuery();
    		  if(res.next()){
    			  return true;
    		  }
    	  }catch(Exception e){
    		  e.printStackTrace();
    		  return false;
    	  }finally{
    		  DBManager.closeAll(conn,state,res);
    	  }
    	  
    	  return false;
      }
    public static boolean registerByUserInfo(UserLogin userinfo){
    	ResultSet res=null;
    	PreparedStatement state=null;
		Connection conn=DBManager.getconnection();
    	String sql="insert into userlogin(UserName,PassWord,UserKind) values(?,?,?);";
    	try{
    		state=conn.prepareStatement(sql);
    		state.setString(1, userinfo.getUserName());
    		state.setString(2,userinfo.getPassWord());
    		state.setString(3, userinfo.getUserKind());
    		int row=state.executeUpdate();
    		if(row>=1){
    			return true;
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    		return false;
    	}finally{
    		DBManager.closeAll(conn,state,res);
    	}
    	return false;
    }

    public static boolean changePassword(String UserName,String PassWord){
    	ResultSet res=null;
    	PreparedStatement state=null;
		Connection conn=DBManager.getconnection();
    	String sql="update userlogin set PassWord=? where UserName=?";
    	try{
    		state=conn.prepareStatement(sql);
    		state.setString(1, PassWord);
    		state.setString(2,UserName);
    		int row=state.executeUpdate();
    		if(row>=1){
    			return true;
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    		return false;
    	}finally{
    		DBManager.closeAll(conn,state,res);
    	}
    	return false;
    }

    public static boolean findUserLogin(UserLogin userLogin) throws SQLException {
		ResultSet res=null;
		PreparedStatement state=null;
		Connection conn=DBManager.getconnection();
		String sql="select * from userlogin  where UserName=?";
		try{
			state=conn.prepareStatement(sql);
			state.setString(1,userLogin.getUserName());
			res=state.executeQuery();
			if(res.next()) {
				System.out.println(res.getString("UserID")+"------------"+res.getString("UserKind"));
				userLogin.setUserID(res.getString("UserID"));
				userLogin.setUserKind(res.getString("UserKind"));
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();

		}finally{
			DBManager.closeAll(conn,state,res);
		}
		return false;
	}


	public static boolean InsertUserIDtoUser(String UserID) throws SQLException {
		ResultSet res=null;
		PreparedStatement state=null;
		Connection conn=DBManager.getconnection();
		String sql="insert  into userresume(UserID) values(?)";
		try{
			state=conn.prepareStatement(sql);
			state.setString(1,UserID);
			int count=state.executeUpdate();
			if(count>=1){
				return true;
			}

		}catch(Exception e){
			e.printStackTrace();

		}finally{
			DBManager.closeAll(conn,state,res);
		}
		return false;
	}

	public static boolean InsertUserIDtoCom(String UserID) throws SQLException {
		ResultSet res=null;
		PreparedStatement state=null;
		Connection conn=DBManager.getconnection();
		String sql="insert  into company(UserID) values(?)";
		try{
			state=conn.prepareStatement(sql);
			state.setString(1,UserID);
			int count=state.executeUpdate();
			if(count>=1){
				return true;
			}

		}catch(Exception e){
			e.printStackTrace();

		}finally{
			DBManager.closeAll(conn,state,res);
		}
		return false;
	}
}

